<?php
/**
 * 默认控制器
 * @author Administrator
 *
 */
class Main extends MY_Controller{
    var $fundType = array();
    function main(){
        parent::MY_Controller();
        $this->fundType = array(
            1 => '单店基金',
            3 => '城市提升基金',
            4 => '车展基金',
            2 => '巡展基金',
            5 => '固定传播基金',
            6 => '专项单店基金',
            7 => '专项城市基金',
        );
        $this->load->model('timeUnitModel');
    }
    /**
     * 根据角色跳转
     */
    function index(){
        if ($this->user['userRole'] == 1){
            redirect(printUrl('Main','storeTotal'));
        }else{
            redirect(printUrl('Main','adminTotal'));
        }
    }
    /**
     * 专营店总控表
     */
    function storeTotal(){
        if ($this->user['userRole'] != 1){
            showError('您无权限进去此页面');
        }
        $this->viewData['title']    = 'RMP汇总表';
        //载入模型
        $this->load->model('singleProcessModel');
        $this->load->model('xunProcessModel');
        $this->load->model('carProcessModel');
        $this->load->model('cityProcessModel');
        $this->load->model('spreadProcessModel');
        $this->load->model('specialProcessModel');
        $this->load->model('specityProcessModel');
        //年份
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        //专营店信息
        $this->load->model('sellpointModel');
        $this->viewData['storeData'] = $this->sellpointModel->getStoreIdData($this->user['userId']);
        //时间单位配置
        $this->viewData['timeUnitList'] = $this->timeUnitModel->getTimeUnitList($year);
        $where = array('storeId'=>$this->user['userId'], 'year'=>$year);
        //单店基金
        $this->viewData['singleProcList'] = $this->singleProcessModel->getNewListByTimeUnit($where);
        //巡展基金
        $this->viewData['xunProcList'] = $this->xunProcessModel->getNewListByTimeUnit($where);
        //车展基金
        $this->viewData['carProcList'] = $this->carProcessModel->getNewListByTimeUnit($where);
        //城市提升基金
        $this->viewData['cityProcList'] = $this->cityProcessModel->getNewListByTimeUnit($where);
        //固定传播基金
        $this->viewData['spreadProcList'] = $this->spreadProcessModel->getNewListByTimeUnit($where);
        //固定传播基金金额汇总
        //$this->viewData['spreadDetail'] = $this->spreadProcessModel->getTotalMoney($this->user['userId'], $year);
        //专项单店基金
        $this->viewData['specialProcList'] = $this->specialProcessModel->getNewListByTimeUnit($where);
        //专项城市基金
        $this->viewData['specityProcList'] = $this->specityProcessModel->getNewListByTimeUnit($where);
        $this->load->view('Main/storeTotal', $this->viewData);
    }
    /**
     * 管理端汇总表
     */
    function adminTotal(){
        if ($this->user['userRole'] == 1){
            showError('您无权限进入此页面');
        }
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('Main', 'adminTotal'),'title'=>'费用总控表');
        //条件
        $viewStoreIds = $this->userLib->getViewStoreIds();
        $where = array();
        if ($viewStoreIds == '*'){
            $where['storeId !='] = 0;
        }else{
            $where['storeId'] = array('in', $viewStoreIds);
        }
        //年
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        $where['year'] = $year;
        //时间单位配置
        $this->viewData['timeUnitList'] = $this->timeUnitModel->getTimeUnitList($year);
        //时间单位
        $this->viewData['timeUnit'] = (int)$this->input->get('timeUnit');
        $this->viewData['timeUnit'] || $this->viewData['timeUnit'] = $this->timeUnitModel->getNowTimeUnit();
        $where['timeUnit'] = $this->viewData['timeUnit'];
        //基金类型
        $fundType = (int)$this->input->get('fundType');
        $fundType || $fundType = 1;
        $this->viewData['fundType'] = $fundType;
        //显示类型
        $showType = (int)$this->input->get('showType');
        $showType || $showType = 1;
        $this->viewData['showType'] = $showType;
        //查找符合条件的主线流程数据
        if ($fundType == 1){//单店基金
            $this->load->model('singleProcessModel');
            $procList = $this->singleProcessModel->getNewListByStoreId($where);
        }elseif ($fundType == 3){//城市基金
            $this->load->model('cityProcessModel');
            $procList = $this->cityProcessModel->getNewListByStoreId($where);
        }elseif ($fundType == 4){//车展基金
            $this->load->model('carProcessModel');
            $procList = $this->carProcessModel->getNewListByStoreId($where);
        }elseif ($fundType == 2){//巡展基金
            $this->load->model('xunProcessModel');
            $procList = $this->xunProcessModel->getNewListByStoreId($where);
        }elseif ($fundType == 5){//固定传播
            $this->load->model('spreadProcessModel');
            unset($where['timeUnit']);
            $procList = $this->spreadProcessModel->getNewListByStoreId($where);
        }elseif ($fundType == 6){//专项单店基金
            $this->load->model('specialProcessModel');
            $procList = $this->specialProcessModel->getNewListByStoreId($where);
        }elseif ($fundType == 7){//专项城市基金
            $this->load->model('specityProcessModel');
            $procList = $this->specityProcessModel->getNewListByStoreId($where);
        }
        $this->viewData['procList'] = $procList;
        //树结构地区列表
        $this->viewData['placeTree'] = $this->userLib->getPlaceTree();
        //获取地区相关缓存数据
        $this->load->model('sellpointModel');
        //小区对应专营店列表
        $spsareaToStoreList = $this->sellpointModel->getCacheData('spsareaToStoreList');
        $this->viewData['spsareaToStoreList'] = $this->sellpointModel->getCacheData('spsareaToStoreList');
        //大区对应专营店列表
        $this->viewData['regionToStoreList'] = $this->sellpointModel->getCacheData('regionToStoreList');
        //地区对应专营店列表
        $this->viewData['areaToStoreList'] = $this->sellpointModel->getCacheData('areaToStoreList');
        $subType = $this->input->get('subType');
        $subType || $subType = 'query';
        $this->viewData['subType'] = $subType;
        //普通查询
        if ($subType == 'query'){
            $this->load->view('Main/adminTotal', $this->viewData);
        }elseif($subType == 'export'){
            $fileName = '';            
            $content = '';
            switch ($fundType.$showType) {
                case '11'://单店小区形式
                    $fileName = '单店基金';
                    $content = $this->load->view('Main/part/singleSpsareaTable', $this->viewData, true);
                    break;
                case '12'://单店大区形式
                    $fileName = '单店基金';
                    $content = $this->load->view('Main/part/singleRegionTable', $this->viewData, true);
                    break;
                case '31'://城市小区形式
                    $fileName = '城市基金';
                    $content = $this->load->view('Main/part/citySpsareaTable', $this->viewData, true);
                    break;
                case '32'://城市大区形式
                    $fileName = '城市基金';
                    $content = $this->load->view('Main/part/cityRegionTable', $this->viewData, true);
                    break;
                case '41'://车展小区形式
                    $fileName = '车展基金';
                    $content = $this->load->view('Main/part/carSpsareaTable', $this->viewData, true);
                    break;
                case '42'://车展大区形式
                    $fileName = '车展基金';
                    $content = $this->load->view('Main/part/carRegionTable', $this->viewData, true);
                    break;
                case '21'://巡展小区形式
                    $fileName = '巡展基金';
                    $content = $this->load->view('Main/part/xunSpsareaTable', $this->viewData, true);
                    break;
                case '22'://巡展大区形式
                    $fileName = '巡展基金';
                    $content = $this->load->view('Main/part/xunRegionTable', $this->viewData, true);
                    break;
                case '51'://固定传播小区形式
                    $fileName = '固定传播基金';
                    $content = $this->load->view('Main/part/spreadSpsareaTable', $this->viewData, true);
                    break;
                case '52'://固定传播大区形式
                    $fileName = '固定传播基金';
                    $content = $this->load->view('Main/part/spreadRegionTable', $this->viewData, true);
                    break;
                case '61'://专项单店小区形式
                    $fileName = '专项单店基金';
                    $content = $this->load->view('Main/part/specialSpsareaTable', $this->viewData, true);
                    break;
                case '62'://专项单店大区形式
                    $fileName = '专项单店基金';
                    $content = $this->load->view('Main/part/specialRegionTable', $this->viewData, true);
                    break;
                case '71'://专项城市小区形式
                    $fileName = '专项城市基金';
                    $content = $this->load->view('Main/part/specitySpsareaTable', $this->viewData, true);
                    break;
                case '72'://专项城市大区形式
                    $fileName = '专项城市基金';
                    $content = $this->load->view('Main/part/specityRegionTable', $this->viewData, true);
                    break;
            }
            $fileName .= '费用总控表';
            if ($showType == 1){
                $fileName .= '-全部';
            }elseif ($showType == 2){
                $fileName .= '-大区';
            }
            $fileName .= $this->viewData['yearList'][$this->viewData['year']];
            $fileName .= '-'.$this->viewData['timeUnitList'][$this->viewData['timeUnit']];
            //设置excel文件下载头
            setExcelDownHeader($fileName);
            echo @ereg_replace("<a [^>]*>|<\/a>", "", $content);
        }
    }
    /**
     * 汇总表明细
     */
    function adminDetail(){
        if ($this->user['userRole'] == 1){
            showError('您无权限进入此页面');
        }
        //查询条件
        $where = array();
        //年
        $this->viewData['year'] = $this->input->get('year');
        $this->viewData['year'] || $this->viewData['year'] = $this->config->item('nowYear');
        $where['year'] = $this->viewData['year'];
        //时间单位
        $this->viewData['timeUnit'] = (int)$this->input->get('timeUnit');
        $this->viewData['timeUnit'] || $this->viewData['timeUnit'] = $this->timeUnitModel->getNowTimeUnit();
        $where['timeUnit'] = $this->viewData['timeUnit'];
        //系统配置
        $this->viewData['yearList'] = $this->timeUnitModel->getCacheData('yearList');
        $timeUnitList = $this->timeUnitModel->getCacheData('timeUnitList');
        $this->viewData['timeUnitList'] = $timeUnitList[$this->viewData['year']];
        //基金类型
        $fundType = (int)$this->input->get('fundType');
        $fundType || $fundType = 1;
        $this->viewData['fundType'] = $fundType;
        $fundTypeName = isset($this->fundType[$fundType]) ? $this->fundType[$fundType] : '';
        //载入模型
        $this->load->model('sellpointModel');
        //地区
        $area = $this->input->get('area');
        $area = urldecode($area);
        $this->viewData['area'] = $area;
        //大区
        $region = $this->input->get('region');
        $region = urldecode($region);
        $this->viewData['region'] = $region;
        //小区
        $spsarea = $this->input->get('spsarea');
        $spsarea = urldecode($spsarea);
        $this->viewData['spsarea'] = $spsarea;
        //关键字
        $keyword = $this->input->get('keyword');
        $keyword = urldecode($keyword);
        $this->viewData['keyword'] = $keyword;
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($area, $region, $spsarea, '', '', 0, $keyword);
        $placeStoreIds = array_keys($placeStoreList);
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        
        if ($viewStoreIds == '*'){
            $where['storeId'] = array('in', $placeStoreIds);
        }else{
            $where['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        //查找符合条件的主线流程数据
        if ($fundType == 1){
            $this->load->model('singleProcessModel');
            $procList = $this->singleProcessModel->getList($where);
            //天网行动
            /* $this->load->model('singleNetModel');
            $netList = $this->singleNetModel->getListByProcId(array('procId'=>array('in', array_keys($procList))));
            foreach ($procList as $k => $v){
                //天网行动双月额度
                $procList[$k]['netBudgetApply'] = isset($netList[$v['id']]) ? $netList[$v['id']]['budgetApply'] : 0;
                $procList[$k]['netSettleApply'] = isset($netList[$v['id']]) ? $netList[$v['id']]['settleApply'] : 0;
                //天网行动可结算金额
                $procList[$k]['netCanSettleMoney'] = isset($netList[$v['id']]) ? $netList[$v['id']]['canSettleMoney'] : 0;
            } */
        }elseif ($fundType == 3){//城市基金
            $this->load->model('cityProcessModel');
            $procList = $this->cityProcessModel->getList($where);
        }elseif ($fundType == 4){//车展基金
            $this->load->model('carProcessModel');
            $procList = $this->carProcessModel->getList($where);
        }elseif ($fundType == 2){//巡展基金
            $this->load->model('xunProcessModel');
            $procList = $this->xunProcessModel->getList($where);
        }elseif ($fundType == 5){//固定传播基金
            unset($where['timeUnit']);
            $this->load->model('spreadProcessModel');
            $procList = $this->spreadProcessModel->getNewListByStoreId($where);
            //获取有效的扣罚列表
            $timeUnitList = $this->spreadProcessModel->getLostMoneyTimeUnitList($procList);
            $this->viewData['timeUnitList'] = $timeUnitList;
            
        }elseif ($fundType == 6){//专项单店基金
            $this->load->model('specialProcessModel');
            $procList = $this->specialProcessModel->getList($where);
        }elseif ($fundType == 7){//专项城市基金
            $this->load->model('specityProcessModel');
            $procList = $this->specityProcessModel->getList($where);
        }
        $this->viewData['procList'] = $procList;
        //所有专营店数据
        $this->viewData['allStoreList'] = $placeStoreList;
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('Main', 'adminTotal',array('year'=>$this->viewData['year'],'timeUnit'=>$this->viewData['timeUnit'],'fundType'=>$this->viewData['fundType'])),'title'=>$fundTypeName.'费用总控表');
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('Main', 'adminList',array('year'=>$this->viewData['year'],'timeUnit'=>$this->viewData['timeUnit'],'fundType'=>$this->viewData['fundType'])),'title'=>'费用明细');
        //提交类型
        $subType = $this->input->get('subType');
        $subType || $subType = 'query';
        $this->viewData['subType'] = $subType;
        if ($subType == 'query'){
            $this->load->view('Main/adminDetail', $this->viewData);
        }elseif($subType == 'export'){
            $fileName = '';
            if ($fundType == 1) {//单店
                $fileName = '单店基金';
                $content = $this->load->view('Main/part/singleDetailTable', $this->viewData, true);
            }elseif ($fundType == 3) {//城市基金
                $fileName = '城市基金';
                $content = $this->load->view('Main/part/cityDetailTable', $this->viewData, true);
            }elseif ($fundType == 4){//车展基金
                $fileName = '车展基金';
                $content = $this->load->view('Main/part/carDetailTable', $this->viewData, true);
            }elseif ($fundType == 2) {//巡展基金
                $fileName = '巡展基金';
                $content = $this->load->view('Main/part/xunDetailTable', $this->viewData, true);
            }elseif ($fundType == 5){//固定传播基金
                $fileName = '固定传播基金';
                $content = $this->load->view('Main/part/spreadDetailTable', $this->viewData, true);
            }elseif ($fundType == 6){//专项单店基金
                $fileName = '专项单店基金';
                $content = $this->load->view('Main/part/specialDetailTable', $this->viewData, true);
            }elseif ($fundType == 7){//专项城市基金
                $fileName = '专项城市基金';
                $content = $this->load->view('Main/part/specityDetailTable', $this->viewData, true);
            }

            $fileName .= '费用明细总控表';
            $area && $fileName .= $area.'区-';
            $region && $fileName .= $region.'-';
            $spsarea && $fileName .= $spsarea.'-';
            $fileName .= $this->viewData['yearList'][$this->viewData['year']];
            if ($fundType != 5){
                $fileName .= '-'.$this->viewData['timeUnitList'][$this->viewData['timeUnit']];
            }
            //设置excel文件下载头
            setExcelDownHeader($fileName);
            echo @ereg_replace("<a [^>]*>|<\/a>", "", $content);
        }
    }
    /**
     * 提交评分系数
     */
    function adminSubScore(){
        $_POST || showError('非法请求');
        $fundType = (int)$this->input->post('fundType');
        $fundType || showError('请选择基金类型');
        (isset($_POST['subData']) && $_POST['subData']) || showError('请设置提交数据');
        $model = '';
        $this->load->model('auditLogModel');
        $type = 0;
        if($fundType == 1){//单店基金评分
            $this->load->model('singleProcessModel');
            $model = $this->singleProcessModel;
            $type = 15;
        }elseif ($fundType == 6){//专项单店基金
            $this->load->model('specialProcessModel');
            $model = $this->specialProcessModel;
            $type = 65;
        }elseif ($fundType == 7){//专项城市基金
            $this->load->model('specityProcessModel');
            $model = $this->specityProcessModel;
            $type = 75;
        }
        foreach ((array)$_POST['subData'] as $k => $v){
            $procData = $model->getData((int)$k);
            if ($fundType == 1 && !$this->auth->isSingleSettleSubScore($procData)){
                showError('您没有评分权限');
            }
            if ($fundType == 6 && !$this->auth->isSpecialSettleSubScore($procData)){
                showError('您没有评分权限');
            }
            if ($fundType == 7 && !$this->auth->isSpecitySettleSubScore($procData)){
                showError('您没有评分权限');
            }
            $model->edit((int)$k, array('score'=>(float)$v['score'],'scoreRole'=>$this->user['userRole']));
            $model->updateMoney((int)$k);
            
            $this->auditLogModel->addLog($type, array(
                'targetId'=>$k,
                'auditRole'=>$this->user['userRole'],
                'auditUid' => $this->user['userId'],
                'auditRs' => 1,
                'auditDes' => '评分操作',
            ));
        }
        showSuccess('提交评分系数成功');
    }
    function test(){
        /*$this->load->model('cityProcessModel');
        $list = $this->cityProcessModel->getList(array('timeUnit'=>2));
        foreach ($list as $k => $v){
            $list = $this->cityProcessModel->budgetUpdateStatus($v['id']);
        }*/
        //广宣统计金额
        $this->load->model('cityGxModel');
        $gxMoney = $this->cityGxModel->getMoney(array('projectId'=>1135));
        //线下活动金额
        $this->load->model('cityOfflineModel');
        $offlineMoney = $this->cityOfflineModel->getMoney(array('projectId'=>1135));
        $this->load->model('cityProjectModel');
        $detailMoney = $this->cityProjectModel->getDetailMoney(1135);
        var_dump($gxMoney,$offlineMoney, $detailMoney);
        /* $this->load->model('singleProcessModel');
        $this->singleProcessModel->updateMoney(62); */
        /*$this->load->model('cityProcessModel');
        $this->cityProcessModel->updateMoney(3);
        $this->load->view('Main/index');*/
    }
    /**
     * 获取IP
     */
    function getip(){
        echo $this->input->ip_address();
    }
    function testEmail(){
        /*$this->load->library('email');
    
        $this->email->from('postmaster@bluedoorinteractive.cn', '东风日产营销网');
        $this->email->to('zhaojianhui129@163.com');
        //$this->email->cc('another@another-example.com');
        //$this->email->bcc('them@their-example.com');
    
        $this->email->subject('Email Test');
        $this->email->message('Testing the email class.');
    
        $this->email->send();
        echo $this->email->print_debugger();*/
        $emailCon = $this->config->item('emailConfig');
        $this->load->library('SMTPMail', $emailCon, 'SMTPMail');
        $title = '测试标题';
        $content = '测试内容';
    
        $this->SMTPMail->SendMail("dflpvmkt@bluedoorinteractive.cn", "421253081@qq.com" ,$title, $content, '东风日产营销网');
    
    }
}